$(document).ready(function() {

    /* General
    ================================================== */
    $("form :submit").button();
    $(".selectAuto").selectmenu();
    
    /* FechaHora
    ================================================== */
    $("#datepicker").datetimepicker({

            timeText: "Hora",	
            hourText: "Horas",
            minuteText: "Minutos",
            currentText: "Hoy",
            closeText: "Listo",
            monthNames: ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],
            dayNamesMin: ["D","L","M","X","J","V","S"],
            dateFormat: "dd-mm-yy",
            stepHour: 1,
            stepMinute: 15,	
            hourMin: 10,
            hourMax: 20,
            hourGrid: 2,
            minuteGrid: 15,
            separator: " a las "

    });

    /* Tratamientos
    ================================================== */
            $.widget( "custom.catcomplete", $.ui.autocomplete, {
            _renderMenu: function( ul, items ) {
                    var self = this,
                            currentCategory = "";
                    $.each( items, function( index, item ) {
                            if ( item.category != currentCategory ) {
                                    ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                                    currentCategory = item.category;
                            }
                            self._renderItem( ul, item );
                    });
            }
    });

    var accentMap = {"á": "a", "é": "e", "í": "i", "ó": "o", "ú": "u"};
    var normalize = function( term ) {
            var ret = "";
            for ( var i = 0; i < term.length; i++ ) {
                    ret += accentMap[ term.charAt(i) ] || term.charAt(i);
            }
            return ret;
    };

    $("#tratamientoInput").catcomplete({
            source: function( request, response ) {
                    var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
                    response( $.grep( tratamientos, function( value ) {
                            value = value.label || value.value || value;
                            return matcher.test( value ) || matcher.test( normalize( value ) );
                    }));
            }
    });

     var oneLetterWidth = 6; //px
     var minCharacters = 10; // input will resize when at least 10 characters are typed

     $('#tratamientoInput').blur(function () {
         var len = $(this).val().length;
         if (len > minCharacters) {// increase width
             $(this).width(len * oneLetterWidth);
         } else {// restore minimal width;
             $(this).width(150);}
     });
    
    /* Especialistas
    ================================================== */
    $("select#selectEspecialistas").selectmenu({
                            icons: [{find: '.avatar-big'}],
                            bgImage: function() {return 'url(' + $(this).attr("title") + ')';},
                            menuWidth: "200px"
                    });

    var tratamientos = [ {label: "Limpieza de Cutis Oxigenante", category: "Tratamientos Faciales"}, {label: "Limpieza de Cutis Isotonica", category: "Tratamientos Faciales"}, {label: "Limpieza de Cutis Dermo-Control", category: "Tratamientos Faciales"}, {label: "Dermalift", category: "Tratamientos Faciales"}, {label: "Choque Anti Fatiga", category: "Tratamientos Faciales"}, {label: "Tinte y Permanente de Pestañas", category: "Tratamientos Faciales"},
                                                     {label: "Manicura", category: "Tratamientos Corporales"},{label: "Pedicura", category: "Tratamientos Corporales"},{label: "Masaje Relajante", category: "Tratamientos Corporales"},{label: "Masaje Circulatorio/Anticelulítico", category: "Tratamientos Corporales"},{label: "Exfoliación", category: "Tratamientos Corporales"}];

    /* Lista - Accordion
    ================================================== */
    var icons = {header: "ui-icon-circle-arrow-e",headerSelected: "ui-icon-circle-arrow-s"};
    var initGrp = {collapsible: true, autoHeight: false, active: false, icons: icons};
    $("#listaClientes").accordion(initGrp);
    $("#listaCitasPendientes").accordion(initGrp);
    $("#listaCitasPendientesFloat").accordion(initGrp);
	
    
    /* Listado Clientes
    ================================================== */
    $(".nombreCHK").change(function(){
        if ($(this).is(":checked")) {$("input[name=nombreCliente]").removeAttr('disabled');}
        else {$("input[name=nombreCliente]").attr("disabled", true);}
    });
    $(".apellidoCHK").change(function(){
        if ($(this).is(":checked")) {$("input[name=apellidoCliente]").removeAttr('disabled');}
        else {$("input[name=apellidoCliente]").attr("disabled", true);}
    });
    $(".generoCHK").change(function(){
        if ($(this).is(":checked")) {$("select[name=generoCliente]").removeAttr('disabled');}
        else {$("select[name=generoCliente]").attr("disabled", true);}
    });
    $(".nickCHK").change(function(){
        if ($(this).is(":checked")) {$("input[name=nickCliente]").removeAttr('disabled');}
        else {$("input[name=nickCliente]").attr("disabled", true);}
    });
    $(".passwordCHK").change(function(){
        if ($(this).is(":checked")) {$("input[name=passwordCliente]").removeAttr('disabled');}
        else {$("input[name=passwordCliente]").attr("disabled", true);}
    }); 
    
});
